from pydantic import BaseModel, field_validator
from typing import Optional
from datetime import datetime

class TranslationRequest(BaseModel):
    """翻译请求模型"""
    text: str
    from_lang: str
    to_lang: str

class TranslationResponse(BaseModel):
    """翻译响应模型"""
    success: bool
    translated_text: Optional[str] = None
    error_message: Optional[str] = None
    from_lang: str
    to_lang: str
    original_text: str

class TranslationHistoryCreate(BaseModel):
    """翻译历史创建模型"""
    source_text: str
    translated_text: str
    source_lang: str
    target_lang: str

class TranslationHistoryOut(BaseModel):
    """翻译历史输出模型"""
    id: int
    source_text: str
    translated_text: str
    source_lang: str
    target_lang: str
    created_at: Optional[str] = None
    user_id: int

    @field_validator('created_at', mode='before')
    @classmethod
    def convert_datetime_to_string(cls, v):
        if isinstance(v, datetime):
            return v.isoformat()
        return v

    class Config:
        from_attributes = True



